package cn.itcast.jx.action.cargo;

import java.util.List;

import org.springframework.ui.Model;

import com.opensymphony.xwork2.ModelDriven;

import cn.itcast.jx.action.BaseAction;
import cn.itcast.jx.domain.ExtCproduct;
import cn.itcast.jx.domain.Factory;
import cn.itcast.jx.service.ExtCproductService;
import cn.itcast.jx.service.FactoryService;
import cn.itcast.jx.util.Page;

public class ExtCproductAction extends BaseAction implements ModelDriven<ExtCproduct>{
	
	private ExtCproductService extCproductService;
	private FactoryService factoryService;
	
	public void setFactoryService(FactoryService factoryService) {
		this.factoryService = factoryService;
	}
	private ExtCproduct model = new ExtCproduct();
	private  String menuType = "extCproduct";
	
	public String getMenuType() {
		return menuType;
	}

	public void setMenuType(String menuType) {
		this.menuType = menuType;
	}
	private Page page = new Page();
	public void setExtCproductService(ExtCproductService extCproductService) {
		this.extCproductService = extCproductService;
	}
	
	public Page getPage() {
		return page;
	}

	public void setPage(Page page) {
		this.page = page;
	}

	public String list() {
		String hql = "from ExtCproduct";
		extCproductService.findPage(hql, page, ExtCproduct.class, null);
		page.setUrl("extCproductAction_list?cwhich=extCproduct");
		push(page);
		return "list";
	}
	
	public String toview() {
		ExtCproduct extCproduct = extCproductService.get(ExtCproduct.class, model.getId());
		push(extCproduct);
		
		return "toview";
	}
	
	public String toupdate() {
		ExtCproduct extCproduct = extCproductService.get(ExtCproduct.class, model.getId());
		String hql = "from Factory where ctype like '附件'";
		 List<Factory> factoryList = factoryService.find(hql, Factory.class, null);
		
		put("factoryList", factoryList);
		push(extCproduct);
		return "toupdate";
	}
	public String update() {
		// 1.根据id,得到要更新的对象
		ExtCproduct obj = extCproductService.get(ExtCproduct.class, model.getId());
		// 2.页面修改的属性，就要更新值
		obj.setFactory(model.getFactory());
		obj.setFactoryName(model.getFactoryName());
		obj.setProductNo(model.getProductNo());
		obj.setProductImage(model.getProductImage());
		obj.setCnumber(model.getCnumber());
		obj.setPackingUnit(model.getPackingUnit());
		obj.setPrice(model.getPrice());
		obj.setOrderNo(model.getOrderNo());
		obj.setProductDesc(model.getProductDesc());
		obj.setProductRequest(model.getProductRequest());
		// 3.更新
		extCproductService.saveOrUpdate(obj);
		return tocreate();

	}
	public String tocreate() {
		String hql2 = "from Factory where ctype like '附件'";
		List<Factory> factoryList = factoryService.find(hql2, Factory.class, null);
		put("factoryList",factoryList);
		
		extCproductService.findPage("from ExtCproduct where contractProduct.id=?", page,ExtCproduct.class,new String[]{model.getContractProduct().getId()});
		page.setUrl("extCproductAction_tocreate");
		push(page);
		
		return "tocreate";
	}
	public String insert() {
		
		extCproductService.saveOrUpdate(model);
		return tocreate();
	}
	
	
	public String delete() {
		extCproductService.delete(ExtCproduct.class, new String[]{model.getId()});
		return tocreate();
	}

	
	@Override
	public ExtCproduct getModel() {
		// TODO Auto-generated method stub
		return model;
	}
}
